package ee.ttu.rakarh.model.data.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import ee.ttu.rakarh.model.data.DataType;
import ee.ttu.rakarh.model.data.entities.Item;
import ee.ttu.rakarh.model.data.entities.ItemAttribute;
import ee.ttu.rakarh.model.data.entities.ItemType;
import ee.ttu.rakarh.util.HibernateUtil;

public class ItemDAOImpl implements ItemDAO {

	private static final Logger LOG = Logger.getLogger(ItemDAOImpl.class);

	@Override
	public Item getItemById(int itemId) {
		Item item = null;
		Session session = null;
		try {
			LOG.info("id=" + itemId);
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			item = (Item) session.load(Item.class, itemId);

		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		return item;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Item> getItemsByTypeId(ItemType type) {
		List<Item> item = new ArrayList<Item>();
		Session session = null;
		try {
			LOG.info("typeId=" + type.getId());
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Criteria crit = session.createCriteria(Item.class);
			item = crit.add(Restrictions.eq("itemType", type)).list();

		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		return item;
	}
	

	@Override
	public void insertItem(Item item) {
		Session session = null;
		try {
			LOG.info("saveing new item");
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Serializable id = session.save(item);
			Set<ItemAttribute> atr = item.getItemAttributes();
			for (ItemAttribute itemAttribute : atr) {
				session.save(itemAttribute);
			}
			session.getTransaction().commit();
			LOG.info("Saved with id = " + id);
		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
	}

	@Override
	public void updateItem(Item item) {
		
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Item itemToUpdate = (Item) session.load(Item.class, item.getId());
			itemToUpdate.setDescription(item.getDescription());
			itemToUpdate.setName(item.getName());
			itemToUpdate.setProducer(item.getProducer());
			itemToUpdate.setProducerCode(item.getProducerCode());
			itemToUpdate.setSalePrice(item.getSalePrice());
			
			Collection<ItemAttribute> itemToUpdateAttributes = itemToUpdate.getItemAttributes();
			Collection<ItemAttribute> itemAttributes = item.getItemAttributes();
			for (ItemAttribute itemAttribute : itemAttributes) {
				for (ItemAttribute itemToUpdateAttribute : itemToUpdateAttributes) {
					if(itemAttribute.getItemAttributeType().getId() == itemToUpdateAttribute.getItemAttributeType().getId()){
						if(itemAttribute.getItemAttributeType().getDataType() == DataType.NUMBER){
							itemToUpdateAttribute.setValueNumber(itemAttribute.getValueNumber());
						}else{
							itemToUpdateAttribute.setValueText(itemAttribute.getValueText());
						}
					}
				}
			}
			
			session.getTransaction().commit();

		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		

	}

	@SuppressWarnings({ "unchecked" })
	@Override
	public Collection<Item> getItemsBySearchName(String string) {
		List<Item> items = new ArrayList<Item>();
		Session session = null;
		try {
			LOG.info("search phrase =" + string);
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Criteria crit = session.createCriteria(Item.class);
			items = crit.add(Restrictions.ilike("name", string, MatchMode.ANYWHERE)).list();

		} catch (Exception e) {
			LOG.error(e);
		} finally {
			//session.close();
		}
		return items;
	}

}
